from selenium.webdriver.support.select import Select
from pages.base_page import BasePage
from config.config import DATAS_PATH
from selenium.webdriver.common.by import By





class CluePage(BasePage):

    url = "/crm/index.php?m=leads"
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    error_locator =(By.XPATH,"/html/body/div/form/fieldset/div[1]")
    clue_pool_locator = (By.CSS_SELECTOR, 'body > div.container > div.page-header > ul > li:nth-child(2) > a')  # 线索池按钮
    add_clue_btn_locator = (By.XPATH, '/html/body/div[5]/div[2]/div[1]/div/a')  # 新建线索按钮
    check_btn_locator = (By.LINK_TEXT, '查看')  # 查看按钮
    first_clue_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[1]/td[1]/input')  # 选择第一个
    second_clue_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[2]/td[1]/input')  # 选择第二个
    fourth_clue_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[3]/td[1]/input')  # 选择第四个
    batch_select_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > ul > li:nth-child('
                                             '1) > div > a')  # 批量操作下拉框按钮
    batch_delete_locator = (By.ID, 'delete')  # 批量删除
    batch_operate_locator = (By.LINK_TEXT, '批量放入线索池')  # 批量放入线索池按钮
    clue_tools_locator = (By.XPATH, '/html/body/div[5]/div[2]/div[1]/div/div/button')  # 线索工具
    import_clue_locator = (By.ID, 'import_excel')  # 导入线索按钮
    skip_locator = (By.ID, 'ownership1')  # 导入线索弹窗中的跳过单选框
    file_locator = (By.XPATH, '//*[@id="attachment1"]/input')  # 选择文件按钮
    owner_name_locator = (By.ID, 'owner_name')  # 负责人输入框
    d_name_locator = (By.ID, 'd_name')  # 选择负责人弹窗的用户名输入框
    search_btn_locator = (By.CSS_SELECTOR, '#dialog-role-list > div > ul > button')  # 选择负责人弹窗的搜索按钮
    first_owner_locator = (By.CSS_SELECTOR, '#d_content > tr > td:nth-child(1) > input[type=radio]')  # 选中第一个负责人
    ok_locator = (By.XPATH, '/html/body/div[12]/div[3]/div/button[1]/span')  # 选择负责人弹窗的ok键
    import_btn_locator = (By.XPATH, '//*[@id="dialog-import"]/form/table/tbody/tr[5]/td[2]/input[1]')  # 导入数据页面的导入按钮
    tips_locator = (By.CSS_SELECTOR, 'body > div.container > div.alert.alert-success')  # 操作后提示语
    recycle_bin_locator = (By.XPATH, '/html/body/div[5]/p/a[14]')  # 回收站
    field_locator = (By.ID, 'field')  # 选择筛选条件
    condition_locator = (By.ID, 'condition')  # 默认值为包含的选择下拉框
    search_locator = (By.ID, 'search')  # 搜索输入框
    do_search_locator = (By.ID, 'dosearch')  # 搜索按钮
    have_inform_locator = (By.CSS_SELECTOR, '#form1 > table')   # 存在数据
    no_inform_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(2)')      # 暂无数据
    # 以下是视图部分的定位器
    all_locator = (By.LINK_TEXT, '全部')  # 全部
    mine_locator = (By.LINK_TEXT, '我负责的')  # 我负责的
    underling_locator = (By.LINK_TEXT, '下属负责的')  # 下属负责的
    underling_create_locator = (By.LINK_TEXT, '下属创建')  # 下属创建
    converted_clue_locator = (By.LINK_TEXT, '已转换线索')  # 已转换线索
    contact_today_locator = (By.LINK_TEXT, '今日需联系')  # 今日需联系
    contact_week_locator = (By.LINK_TEXT, '本周需联系')  # 本周需联系
    contact_month_locator = (By.LINK_TEXT, '本月需联系')  # 本月需联系
    not_contacted7th_locator = (By.LINK_TEXT, '7日未联系')  # 7日未联系
    not_contacted15th_locator = (By.LINK_TEXT, '15日未联系')  # 15日未联系
    not_contacted30th_locator = (By.LINK_TEXT, '30日未联系')  # 30日未联系
    recently_created_locator = (By.LINK_TEXT, '最近创建')  # 最近创建
    recently_modified_locator = (By.LINK_TEXT, '最近修改')  # 最近修改

    def click_clue_pool(self):
        """
        线索池按钮的点击
        :return: None
        """
        self.find_element(self.clue_pool_locator).click()

    def click_add_clue(self):
        """
        点击新建线索
        :return: None
        """
        self.find_element(self.add_clue_btn_locator).click()

    def click_check_btn(self):
        """
        点击查看
        :return: None
        """
        self.find_element(self.check_btn_locator).click()

    def click_first_clue(self):
        """
        选择第一条线索
        :return: None
        """
        self.find_element(self.first_clue_locator).click()

    def click_second_clue(self):
        """
        选择中第二条线索
        :return: None
        """
        self.find_element(self.second_clue_locator).click()

    def click_fourth_clue(self):
        """
        选中第三条线索
        :return: None
        """
        self.find_element(self.fourth_clue_locator).click()

    def click_batch_select(self):
        """
        点击批量操作
        :return: None
        """
        self.find_element(self.batch_select_locator).click()

    def click_batch_delete(self):
        """
        点击批量删除
        :return: None
        """
        self.find_element(self.batch_delete_locator).click()

    def click_batch_operate(self):
        """
        批量放入线索池
        :return: None
        """
        self.find_element(self.batch_operate_locator).click()

    def click_clue_tools(self):
        """
        点击线索工具
        :return: None
        """
        self.find_element(self.clue_tools_locator).click()

    def click_import_clue(self):
        """
        点击导入线索按钮
        :return: None
        """
        self.find_element(self.import_clue_locator).click()

    def click_skip(self):
        """
        选中跳过
        :return: None
        """
        self.find_element(self.skip_locator).click()

    def input_file(self, filename):
        """
        输入文件路径
        :param filename:
        :return: None
        """
        self.find_element(self.file_locator).send_keys(self._get_abs_file(filename))

    def click_owner_name(self):
        """
        点击负责人选框
        :return: None
        """
        self.find_element(self.owner_name_locator).click()

    def input_d_name(self, search_key):
        """
        选择负责人弹窗中：输入搜索的关键词
        :param search_key:
        :return: None
        """
        self.find_element(self.d_name_locator).send_keys(search_key)

    def click_owner_search_btn(self):
        """
        在选择负责人的弹窗点击搜索
        :return: None
        """
        self.find_element(self.search_btn_locator).click()

    def click_first_owner(self):
        """
        在选择负责人的弹窗选中第一个负责人
        :return: None
        """
        self.find_element(self.first_owner_locator).click()

    def click_ok(self):
        """
        在选择负责人的弹窗点击ok
        :return: None
        """
        self.find_element(self.ok_locator).click()

    def click_import_btn(self):
        """
        在导入数据的弹窗点击导入
        :return: None
        """
        self.find_element(self.import_btn_locator).click()

    def click_recycle_bin(self):
        """
        点击回收站
        :return: None
        """
        self.find_element(self.recycle_bin_locator).click()

    def select_field(self, field):
        """
        选择筛选条件
        :param field:
        :return: None
        """
        field_element = Select(self.find_element(self.field_locator))
        field_element.select_by_visible_text(field)

    def select_condition(self, condition):
        """
        选择条件
        :param condition:
        :return:None
        """
        condition_element = Select(self.find_element(self.condition_locator))
        condition_element.select_by_visible_text(condition)

    def input_search_key(self, clue_search_key):
        """
        输入搜索词
        :param clue_search_key:
        :return: None
        """
        self.find_element(self.search_locator).send_keys(clue_search_key)

    def click_do_search(self):
        """
        点击搜索按钮，搜索线索
        :return: None
        """
        self.find_element(self.do_search_locator).click()

    def have_inform_text(self):
        """
        搜索后存在数据
        :return: 返回搜索到的数据
        """
        return self.find_element(self.have_inform_locator).text

    def no_inform_text(self):
        """
        暂无数据
        :return: 返回搜索到的数据
        """
        return self.find_element(self.no_inform_locator).text

    def click_all(self):
        """
        点击视图——全部
        :return: None
        """
        self.find_element(self.all_locator).click()

    def click_mine(self):
        """
        点击视图——我负责的
        :return: None
        """
        self.find_element(self.mine_locator).click()

    def click_underling(self):
        """
        点击视图——下属负责的
        :return: None
        """
        self.find_element(self.underling_locator).click()

    def click_underling_create(self):
        """
        点击视图——我负责的
        :return: None
        """
        self.find_element(self.underling_create_locator).click()

    def click_converted_clue(self):
        """
        点击视图——已转换线索
        :return: None
        """
        self.find_element(self.converted_clue_locator).click()

    def click_contact_today(self):
        """
        点击视图——今日需联系
        :return: None
        """
        self.find_element(self.contact_today_locator).click()

    def click_contact_week(self):
        """
        点击视图——本周需联系
        :return: None
        """
        self.find_element(self.contact_week_locator).click()

    def click_contact_month(self):
        """
        点击视图——本月需联系
        :return: None
        """
        self.find_element(self.contact_month_locator).click()

    def click_not_contact7th(self):
        """
        点击视图——7日未联系
        :return: None
        """
        self.find_element(self.not_contacted7th_locator).click()

    def click_not_contact15th(self):
        """
        点击视图——15日未联系
        :return: None
        """
        self.find_element(self.not_contacted15th_locator).click()

    def click_not_contact30th(self):
        """
        点击视图——30日未联系
        :return: None
        """
        self.find_element(self.not_contacted30th_locator).click()

    def click_recently_create(self):
        """
        点击视图——最近创建
        :return: None
        """
        self.find_element(self.recently_created_locator).click()

    def click_recently_modified(self):
        """
        点击视图——最近修改
        :return: None
        """
        self.find_element(self.recently_modified_locator).click()

#返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text

#返回弹窗文本信息
    def get_alert_text(self):
        return self.driver.switch_to.alert.text

#返回报错弹窗文本信息
    def get_error_text(self):
        return self.find_element(self.error_locator).text


